package com.xd.core.bill.controller;


import com.github.pagehelper.Page;
import com.xd.common.constant.SysConstant;
import com.xd.common.util.SecurityUtil;
import com.xd.core.api.service.IRepaymentOrderInfoService;
import com.xd.core.bill.dto.*;
import com.xd.core.bill.service.IBillService;
import com.xd.core.bill.vo.*;
import com.xd.core.business.service.IGuaranteeSlipService;
import com.xd.core.business.service.IInformationService;
import com.xd.core.debt.dto.ApplyCertificateDto;
import com.xd.core.debt.dto.DebtOneDto;
import com.xd.core.debt.service.IDebtService;
import com.xd.core.mission.entity.MissionCenterInfo;
import com.xd.core.mission.mapper.MissionCenterInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 账单管理
 * </p>
 *
 * @author ZCH
 * @since 2025-02-26
 */
@Api(tags = "账单管理")
@RestController
@RequestMapping("/core/bill")
@Slf4j
public class BillController {

    @Autowired
    private IBillService iBillService;
    @Autowired
    private MissionCenterInfoMapper missionCenterInfoMapper;
    @Autowired
    private IDebtService debtService;
    @Autowired
    private IGuaranteeSlipService iGuaranteeSlipService;
    @Autowired
    private IRepaymentOrderInfoService iRepaymentOrderInfoService;
    @Autowired
    private IInformationService informationService;

    @PreAuthorize("hasAnyAuthority('sys:BillList-viewDetails','sys:BillList')")
    @PostMapping("/billList")
    @ApiOperation("账单列表分页查询")
    public Page<BillPageVo> getBillList(@RequestBody BillQuery billQuery) {
        return iBillService.getBillList(billQuery);
    }
    @PreAuthorize("hasAnyAuthority('sys:AccountsReceivableList-viewDetails')")
    @GetMapping("/billDetail/{id}")
    @ApiOperation("账单详情和应收款详情")
    public BillDetailVo getBillDetail(@PathVariable Integer id) {
        return iBillService.getBillDetail(id);
    }

    @PreAuthorize("hasAnyAuthority('sys:AccountsReceivableList-viewDetails','sys:AccountsReceivableList')")
    @PostMapping("/receivePaymentList")
    @ApiOperation("应收款列表")
    public Page<ReceivePaymentPageVo> getReceivePaymentList(@RequestBody ReceivePaymentQuery query) {
        return iBillService.getReceivePaymentList(query);
    }

    @PreAuthorize("hasAnyAuthority('sys:RepaymentManagement-viewDetails')")
    @PostMapping("/earlyRepaymentList")
    @ApiOperation("提前还款列表")
    public Page<EarlyRepaymentPageVo> getEarlyRepaymentList(@RequestBody EarlyRepaymentQuery query) {
        return iBillService.getEarlyRepaymentList(query);
    }

    @PostMapping("/earlyRepaymentDetail")
    @ApiOperation("提前还款详情")
    public EarlyRepaymentDetailVo getEarlyRepaymentDetail(@RequestBody DebtOneDto debtOneDto) {
        return iBillService.getEarlyRepaymentDetail(debtOneDto);
    }

    @PreAuthorize("hasAnyAuthority('sys:BillList-Export')")
    @ApiOperation("账单列表导出")
    @PostMapping("/exportBillList")
    public void exportBillList(@RequestBody BillQuery query) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        iBillService.exportBillList(query, missionCenterInfo.getId());
    }

    @PreAuthorize("hasAnyAuthority('sys:AccountsReceivableList-Export')")
    @ApiOperation("应收款列表导出")
    @PostMapping("/exportReceivePaymentList")
    public void exportReceivePaymentList(@RequestBody ReceivePaymentQuery query) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        iBillService.exportReceivePaymentList(query, missionCenterInfo.getId());
    }

    @ApiOperation("提前还款列表导出")
    @PostMapping("/exportEarlyRepaymentList")
    public void exportEarlyRepaymentList(@RequestBody EarlyRepaymentQuery query) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);
        //获取数据
        iBillService.exportEarlyRepaymentList(query, missionCenterInfo.getId());
    }


    @PreAuthorize("hasAnyAuthority('sys:RepaymentManagement-ExecutionApplication')")
    @ApiOperation("申请公证书")
    @PostMapping("/applyCertificate")
    public void applyCertificate(@RequestBody ApplyCertificateDto dto) {
        debtService.applyCertificate(dto);
        log.info("修改申请公证书状态成功");
    }

    @PostMapping("/offlineRepaymentDetail")
    @ApiOperation("线下还款数据回显")
    public OfflineRepaymentDetailVo getOfflineRepaymentDetail(@RequestBody OfflineRepaymentDetailQuery query) {
        return iRepaymentOrderInfoService.getOfflineRepaymentDetail(query);
    }

    @PostMapping("/overduePolicyRefund")
    @ApiOperation("退保列表")
    public Page<OverduePolicyRefundVo> getOverduePolicyRefundList(@RequestBody OverduePolicyRefundQuery query) {
        return iGuaranteeSlipService.getOverduePolicyRefundList(query);
    }

    @GetMapping("/viewVouchers/{surrenderId}")
    @ApiOperation("获取退保凭证")
    public List<String> getVouchers(@PathVariable Integer surrenderId) {
        return informationService.getVouchers(surrenderId);
    }

}
